1 // pages/newsAndActivityDetails/newsAndActivityDetails.js
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    status: 'usual',
    url: app.data.imgUrl,
    id: '', // 新闻详情的id
    newsType: '', // 是新闻呢还活动
    commentListParams: {
      id: '',
      page: 1,
      numPerPage: 10,
      sort: 2
    },
    commentlist: [],
    detail: null,
    focusNum: null,
    commentParams: {
      newsId: '',
      content: ''
    },
    // 当前是回复还是评论
    isReply: false,
    commentfocus: false,
    replyfocus: false,
    scrollToComment: '',
    adminUserInfo: null, //管理员信息
    isBlacklist: false, //是否拉黑
    isAdminLogin: false, //是否是管理员登录
    oneSynchroData: {
      id: '', //评论id
      oneCommentNum: 0,
      dainzan: 0
    },
    userInfo: null,
    isLoodingHide: true//加载动画
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    let newsId = options.id // 新闻id
    let commentId = options.commentId || '' // 评论id
    let floorNum = options.floorNum // 楼层id
    let page = 1
    let numPerPage = 10
    this.data.oneSynchroData.id = options.id //设置newsid

    //获取用户Id
    // let accId = wx.getStorageSync('loginInfo').accId
    // this.getUserDetail(accId)

    if (floorNum) {
      page = Math.ceil(floorNum / numPerPage)
    }
    this.setData({
      id: options.id,
      commentId: commentId,
      'commentListParams.id': options.id,
      'commentListParams.page': page,
      'commentListParams.numPerPage': numPerPage,
      newsType: options.type,
      'commentParams.newsId': newsId
    })
    this.getDetail(this.data.id)
    let params = {
      id: this.data.id,
      page: 1,
      numPerPage: 5
    }
    this.getFocusNum(params)
    this.getComment(this.data.commentListParams)
    var that = this
    if (this.data.commentId) {
      setTimeout(() => {
        that.setData({
          scrollToComment: `c_${this.data.commentId}`
        })
      }, 2000)
    }
    //获取管理员登录状态
    this.data.isAdminLogin = wx.getStorageSync('teamLogin') //管理员登录状态
    //获取管理员信息
    this.data.adminUserInfo = wx.getStorageSync("adminUserInfo") //获取管理员信息
    this.getAdminDetail(this.data.adminUserInfo.associationId)

  },
  //获取用户信息
  // getUserDetail(id) {
  //   wx.teamApi.account.info(id).then(res => {
  //     this.setData({
  //       userInfo: res.data.result
  //     })
  //   })
  // },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    //start进入二级评论页面，同步数据
    if (app.globalData.secondarySynchroData) {
      //遍历一级评论 设置其值
      for (var item in this.data.commentlist) {
        console.log(this.data.commentlist[item])
        if (this.data.commentlist[item].id == app.globalData.secondarySynchroData.id) {
          this.data.commentlist[item].upNum += app.globalData.secondarySynchroData.dainzan
          if (this.data.commentlist[item].upStatus == 1 && app.globalData.secondarySynchroData.dainzan < 0) {
            this.data.commentlist[item].upStatus = 0
          } else if (this.data.commentlist[item].upStatus == 0 && app.globalData.secondarySynchroData.dainzan > 0) {
            this.data.commentlist[item].upStatus = 1
          }
          this.data.commentlist[item].replyNum += app.globalData.secondarySynchroData.secondaryCommentNum
          this.setData({
            'detail.content.commentNum': this.data.detail.content.commentNum + app.globalData.secondarySynchroData.secondaryCommentNum
          })
        }
      }
      this.setData({
        commentlist: this.data.commentlist
      })
      app.globalData.secondarySynchroData = null
    }
    //end进入二级评论页面，同不数据
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {
    this.data.oneSynchroData.oneCommentNum = this.data.detail.content.commentNum
    app.globalData.oneSynchroData = this.data.oneSynchroData
    
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {
    this.data.commentListParams.page++
      this.getComment(this.data.commentListParams)
  },
  loadmoreComment() {
      this.setData({
        isLoodingHide:false
      })
    this.data.commentListParams.page++
      this.getComment(this.data.commentListParams)
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {
    return {
      title: '我发现了一个比较有趣的' + this.data.newsType + '，快来围观',
      imgUrl: 'https://aa-1257564155.cos.ap-chengdu.myqcloud.com/tmp_955d50b786dc9776a422b990a07f26cc.jpg',
      path: '/pages/login/index?url=/pages/newsDetail/index&id=' + this.data.id
    }
  },
  //排序事件
  sort(e) {
    this.data.commentListParams.sort = e.currentTarget.dataset.sort
    console.log('189')
    console.log(e.currentTarget.dataset.sort)
    this.setData({
      commentlist: [],
      commentListParams: this.data.commentListParams
    })
    this.data.commentListParams.page = 1
    this.getComment(this.data.commentListParams)
  },
  //页面跳转
  goto(e) {
    let url = e.currentTarget.dataset.url
    wx.navigateTo({
      url: url,
    })
  },
  //点赞事件
  zanClick() {
    if (this.data.detail.content.upStatus) {
      this.cancelUpNews(this.data.detail.content.id)
    } else {
      this.upNews(this.data.detail.content.id)
    }
  },
  //加上
  upNews(id) {
    wx.teamApi.news.upNews(id).then(res => {
      this.setData({
        'detail.content.upStatus': true,
        'detail.content.upNum': this.data.detail.content.upNum += 1
      })
      this.data.oneSynchroData.dainzan += 1
    })
  },
  //减去
  cancelUpNews(id) {
    wx.teamApi.news.cancelUpNews(id).then(res => {
      this.setData({
        'detail.content.upStatus': false,
        'detail.content.upNum': this.data.detail.content.upNum -= 1
      })
      this.data.oneSynchroData.dainzan -= 1
    })
  },
  comment() {
    if (this.data.detail.accBlacklist == false || this.data.isAdminLogin) {
      this.setData({
        status: 'comment',
        isReply: false,
        commentfocus: true
      })
    } else {
      wx.showToast({
        title: '已被该社团拉黑！不能进行评论！',
        icon: 'none'
      })
    }
  },
  contentInput(e) {
    this.setData({
      'commentParams.content': e.detail.value
    })
    console.log(this.data.commentParams)
  },
  //查看图片大图
  previewImage(e) {
    let index = e.currentTarget.dataset.index
    let urls = []
    for (let i = 1; i < 10; i++) {
      let url = this.data.detail.content['pic' + i]
      if (url) {
        urls.push(url)
      }
    }
    wx.previewImage({
      urls: urls,
      current: urls[index]
    })
  },
  //申请加入活动
  joinActive() {
    if (this.data.detail.accBlacklist == false) {
      wx.teamApi.news.applyactivity(this.data.id).then(res => {
        if (res.data.errcode === 0) {
          if (res.data.result === 0) {
            wx.showToast({
              icon: 'loding',
              title: '申请中..',
            })
            this.setData({
              'detail.content.joinStatus': 0
            })
          } else if (res.data.result === -1) {
            wx.showToast({
              icon: 'none',
              title: '已经被拒绝',
            })
            this.setData({
              'detail.content.joinStatus': -1
            })
          } else if (res.data.result === 1) {
            wx.showToast({
              title: '申请成功！',
            })
            this.getDetail(this.data.id)
          }
        }
      })
    } else {
      wx.showToast({
        title: '已被该社团拉黑！不能申请加入！',
        icon: 'none'
      })
    }

  },
  upvote(e) {
    let data = e.currentTarget.dataset.item
    let index = e.currentTarget.dataset.index
    let type = e.currentTarget.dataset.type
    if (data.upStatus) {
      this.cancelUpComment(data.id, index, type)
    } else {
      this.upComment(data.id, index, type)
    }
  },
  upComment(id, index, type) {
    wx.teamApi.news.upComment(id).then(res => {
      if (res.data.errcode === 0) {
        this.data.commentlist[index].upStatus = 1
        this.data.commentlist[index].upNum += 1
        this.setData({
          commentlist: this.data.commentlist
        })
      }
    })
  },
  cancelUpComment(id, index, type) {
    wx.teamApi.news.cancelUpComment(id).then(res => {
      if (res.data.errcode === 0) {
        this.data.commentlist[index].upStatus = 0
        this.data.commentlist[index].upNum -= 1
        this.setData({
          commentlist: this.data.commentlist
        })
      }
    })
  },
  //根据管理员id获取详细信息
  getAdminDetail(id) {
    wx.teamApi.association.detail(id).then(res => {
      this.setData({
        adminUserInfo: res.data.result
      })

      console.log('338')
      console.log(this.data.adminUserInfo)
    })
  },
  //评论楼主事件
  bindButtonTap() {
    console.log('340')
    console.log(this.data.commentParams)
    wx.teamApi.news.postcomment(this.data.commentParams).then(res => {
      if (res.data.errcode === 0) {
        wx.showToast({
          title: '评论成功',
        })
        //评论成功
        var comments=[res.data.result]
        console.log(comments)
        let arr = comments.concat(this.data.commentlist)
        this.setData({
          commentlist: arr,
          'commentParams.content':'',
          'detail.content.commentNum': this.data.detail.content.commentNum+1
        })

      }
    })
  },
  //根据id查询新闻详情
  getDetail(id) {
    wx.teamApi.news.detail(id).then(res => {
      if (res.data.errcode === 0) {
        this.setData({
          detail: res.data.result
        })
      } else {
        wx.showModal({
          title: '提示',
          content: res.data.err,
          success: () => {
            wx.navigateBack({
              delta: 1
            })
          }
        })
      }
      console.log('388')
      console.log(this.data.detail)
    })
  },
  //获取参与人数
  getFocusNum(params) {
    wx.teamApi.news.focusNum(params).then(res => {
      if (res.data.errcode === 0) {
        this.setData({
          focusNum: res.data.result
        })
      } else {
        wx.showModal({
          title: '提示',
          content: res.data.err,
          success: () => {
            wx.navigateBack()
          }
        })
      }
    })
  },

  //获取评论
  getComment(params, bool) {
    bool = !!bool
    wx.teamApi.news.commentlist(params).then(res => {
      console.log('413')
      console.log(params.sort)
      console.log(res)
      if (res.data.result.length <= 0) {
        if (this.data.commentListParams.page > 1) {
          this.data.commentListParams.page--
        }
      }
      if (bool) {
        this.setData({
          commentlist: res.data.result
        })
      } else {
        let arr = this.data.commentlist.concat(res.data.result)
        this.setData({
          commentlist: arr
        })
      }
      this.setData({
        isLoodingHide:true
      })
    })
  },
  inputBlur() {
    this.setData({
      status: 'usual'
    })
  },
  //拉黑用户
  setBlacklist(e) {
    console.log('438')
    console.log(this.data.adminUserInfo)
    console.log(this.data.detail.content)
    console.log(this.data.isAdminLogin)
    //[1]判断当前新闻是否是本人管理员
    if (this.data.isAdminLogin && this.data.adminUserInfo.id == this.data.detail.content.associationId) {
      //【2】获取该评论用户ID
      let accId = e.currentTarget.dataset.info.accId //id
      console.log('442')
      console.log(e.currentTarget.dataset.info)
      //[3]判断被拉黑用户是否是自己
      if (e.currentTarget.dataset.info.isAssociation == 0) {
        wx.showModal({
          title: '提示',
          content: '是否拉黑该用户？',
          success: (res) => {
            if (res.confirm) { //用户点击确定
              //调用拉黑接口
              wx.teamApi.account.setBlacklist(accId, 0).then(res => {
                //成功设置
                this.data.isBlacklist = true
              })
            }
          }
        })
      } else {
        wx.showToast({
          title: '管理员账号不能被拉黑！',
          icon: 'none'
        })
      }
    }
  },
  //删除评论
  deleteComment(e) {
    //[2]判断是否是管理员
    if (this.data.isAdminLogin && this.data.adminUserInfo.id == this.data.detail.content.associationId) {
      //【3】获取该评论用户ID
      let accId = e.currentTarget.dataset.info.accId
      //[4]获取评论id
      let commentId = e.currentTarget.dataset.info.id
      //获取下标
      let commentindex = e.currentTarget.dataset.commentindex

      console.log('477')
      console.log(this.data.commentlist[commentindex].replyNum)
      //判断当前评论用户是否是管理员
      if (true) { //不是才执行
        //删除评论
        wx.showModal({
          title: '提示',
          content: '是否删除该条评论',
          success: (res) => {
            if (res.confirm) { //确定
              //获取评论id
              let id = e.currentTarget.dataset.info.id
              //调用删除接口
              wx.teamApi.news.deletecomment(accId, commentId, this.data.id).then(res => {
                if (res.data.errcode === 0) {
                  var arr = this.data.commentlist
                  let replyNum = arr[commentindex].replyNum+1
                  arr.splice(commentindex, 1)
                  this.setData({
                    commentlist: arr,
                    'detail.content.commentNum': this.data.detail.content.commentNum-replyNum
                  })
                  
                }                
              })
            }
          }
        })
      }
    }
  },
  //跳转到评论详情事件
  gotoCommentDetail(e) {
    let url = e.currentTarget.dataset.url
    //把当前评论保存到全局中item
    app.globalData.commentlist = e.currentTarget.dataset.item
    app.globalData.commentlist.accBlacklist = this.data.detail.accBlacklist
    app.globalData.commentlist.associationId = this.data.detail.content.associationId
    wx.navigateTo({
      url: url,
    })
  }
})